package com.youxin.busfoundation.email;

import java.text.ParseException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.util.StringUtils;

import com.youxin.busfoundation.bean.MonitorEmailBean;
import com.youxin.busfoundation.config.MonitorEmailConfig;
import com.youxin.foundation.lang.BaseException;

/**
 * 邮件发送功能
 * 
 * @author yingde.cao
 *
 */
public class EmailController {

	private static EmailController instance;
	private static Object lockObj = new Object();

	private MonitorEmailBean monitorEmailConfig;

	public EmailController(MonitorEmailBean monitorEmailConfig) {
		this.monitorEmailConfig = monitorEmailConfig;
	}

	/**
	 * 程序启动时加载Email的配置
	 * 
	 * @param conf
	 */
	public static void initConfig(MonitorEmailBean monitorEmailConfig) throws ParseException {
		synchronized (lockObj) {
			if (instance == null) {
				instance = new EmailController(monitorEmailConfig);
			}
		}
	}

	public static void sendEmail(String subject, String content)
			throws MessagingException, AddressException, BaseException {

		if (instance == null) {
			throw new BaseException(0, "请初始化邮件配置!");
		}
		instance.sendMessage(subject, content);
	}

	public void sendMessage(String subject, String content) throws MessagingException, AddressException
	{
		sendMessage(subject, content,false);
	}
	
	public void sendMessage(String subject, String content,boolean isHtml) throws MessagingException, AddressException {
		final String username = monitorEmailConfig.getUsername();
		final String password = monitorEmailConfig.getPassword();

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", monitorEmailConfig.getSmtp());

		if (monitorEmailConfig.getSsl() == 0) {
			props.put("mail.smtp.port", String.valueOf(monitorEmailConfig.getPort()));
		} else {
			props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
			props.put("mail.smtp.socketFactory.port", String.valueOf(monitorEmailConfig.getPort()));
			props.put("mail.smtp.port", String.valueOf(monitorEmailConfig.getPort()));
		}

		Session session = Session.getInstance(props, new javax.mail.Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});

		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(username));
		message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(monitorEmailConfig.getTo()));
		if(!StringUtils.isEmpty(monitorEmailConfig.getCc()))
		{
			message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(monitorEmailConfig.getCc()));
		}
		message.setSubject(subject);
		if(isHtml)
		{
			message.setContent(content, "text/html;charset = utf-8");  
		}else{
			message.setText(content);
		}

		Transport.send(message);

	}
}
